﻿$(document).ready(function(){
	inputEventStyle(".form_panel", ".error_panel");

	$("#password").bind("keydown", function(event){
		event = event ? event : (window.event ? window.event : null);
		if(event.keyCode == 13){
			loginIn();
		};
	});
	$("#loginSubset").bind("click", function(){
		loginIn();
	});
});

function showInfo(str){
	$("#msg").text(str).parent().show();
}

function hideInfo(){
	$("#msg").parent().hide();
}

function inputEventStyle(obj,errObj){
	var $input = $(obj).children("label").children("input");
	$input.each(function(){
		if($(this).val()!=""){
			$(this).prev("pre").hide();
		}
	}).bind("keydown", function(){
		$(errObj).hide();
		$(this).prev("pre").hide();
	}).bind("blur", function(){
		if($.trim($(this).val())==""){
			$(this).prev("pre").show();
		}
	}).bind("focus", function(){
		hideInfo();
	}).first().focus();
}

function loginIn(){
	if($("#loginSubset").hasClass("a_as_disabled")){
		return false;
	}
	var userName = $.trim($("#userName").val()),
		password = $.trim($("#password").val());
	if(userName == "" && password == ""){
		showInfo("Username & Password is empty");
		return false;
	}
	$("#loginSubset").addClass("a_as_disabled");
	$.ajax({
		type : "POST",
		url : look._URL,
		contentType : "application/json;charset=UTF-8",
		dataType : "json",
		data : look._LOGIN_REGISTER_JSON.Set(userName, password),
		timeout : 15000,
		beforeSend : function(XMLHttpRequest){
			XMLHttpRequest.setRequestHeader("usb", "9528");
		},
		success : function(data, textStatus, XMLHttpRequest){
			if(data != null){
				if(data.code == "200" || data.code == "205"){
					$.cookie("admin_info","u=" + data.user.user_id + "&t=" + data.user.token);
					window.location.href = "index.html";
				}else if(data.code){
					showInfo(data.message);
				}
			}else{
				showInfo("网络连接出错！");
			}
		},
		error : function(XMLHttpRequest, error){
			if(error == "timeout"){
				showInfo("请求超时：请求系统返回数据超时！");
			}else{
				showInfo(error + "：请求出错！");
			}
		},
		complete : function(XMLHttpRequest, textStatus){
			$("#loginSubset").removeClass("a_as_disabled");
		}
	});
}
